package com.hainatong.shiphelper.mainhome.fragment.massage;

import java.util.ArrayList;
import java.util.List;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.mainhome.adapter.SysMsgAdapter;
import com.hainatong.shiphelper.mainhome.entity.MsgBean;
import com.hainatong.shiphelper.mainhome.entity.SysMsgTime;
import com.hainatong.shiphelper.mainhome.viewmodel.MsgViewModel;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.hainatong.mvvmbase.base.BaseBean;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

public class SysMsgFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, MsgViewModel>
{
    private SysMsgAdapter adapter;

    private List<BaseBean> mSysMsgList = new ArrayList<>();

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        adapter = new SysMsgAdapter();
        viewDataBinding.refreshlayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager(getContext()));
        viewDataBinding.recyclerview.addItemDecoration( new ListItemDecoration(SizeUtils.dp2px( 10f )));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener( (adapter1, view1, position) -> {
        } );


        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {
            viewModel.tryRefreshSysMsg();
        } );

        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
            viewModel.loadMoreSysMsg();
        } );

    }


    @Override
    protected void lazyLoad()
    {
        viewModel.querySysMsg();
        // list 结果
        viewModel.getMsgListResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        mSysMsgList.clear();
                        //处理 结果
                        addNewSysMsgData(mSysMsgList,result.getData());
                        adapter.setList(mSysMsgList);
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        List<BaseBean> moreSysMsgs = setMoreSysMsgData( result.getData() );

                        adapter.addData( moreSysMsgs );
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });

    }

    private void addNewSysMsgData(List<BaseBean> msgList , List<MsgBean> sysMsgList)
    {
        if( sysMsgList != null && !sysMsgList.isEmpty() )
        {
            for(MsgBean msg : sysMsgList  )
            {
                SysMsgTime msgTime = new SysMsgTime();
                msgTime.setTime( msg.getCreateDate() );
                msgList.add(msgTime);
                msgList.add( msg );
            }
        }
    }


    private List<BaseBean> setMoreSysMsgData( List<MsgBean> sysMsgList)
    {
        List<BaseBean> moreSysMsgList = new ArrayList<>();
        if( sysMsgList != null && !sysMsgList.isEmpty() )
        {
            for(MsgBean msg : sysMsgList  )
            {
                SysMsgTime msgTime = new SysMsgTime();
                msgTime.setTime( msg.getCreateDate() );
                moreSysMsgList.add(msgTime);
                moreSysMsgList.add( msg );
            }
        }
        return moreSysMsgList;
    }
}
